// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraGamePhaseAbility.h" // 包含头文件
#include "AbilitySystemComponent.h" // 包含能力系统组件
#include "Engine/World.h" // 包含世界引擎
#include "LyraGamePhaseSubsystem.h" // 包含游戏阶段子系统

#if WITH_EDITOR
#include "Misc/DataValidation.h" // 包含数据验证相关头文件
#endif

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraGamePhaseAbility) // 包含自动生成的代码

#define LOCTEXT_NAMESPACE "ULyraGamePhaseAbility" // 定义本地化文本命名空间

/**
 * 构造函数，初始化游戏阶段技能的默认属性
 */
ULyraGamePhaseAbility::ULyraGamePhaseAbility(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer) // 调用父类构造函数
{
	ReplicationPolicy = EGameplayAbilityReplicationPolicy::ReplicateNo; // 设置复制策略为不复制
	InstancingPolicy = EGameplayAbilityInstancingPolicy::InstancedPerActor; // 设置实例化策略为每个Actor实例化
	NetExecutionPolicy = EGameplayAbilityNetExecutionPolicy::ServerInitiated; // 设置网络执行策略为服务器发起
	NetSecurityPolicy = EGameplayAbilityNetSecurityPolicy::ServerOnly; // 设置网络安全策略为仅服务器
}

/**
 * 激活能力时的具体实现
 */
void ULyraGamePhaseAbility::ActivateAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, const FGameplayEventData* TriggerEventData)
{
	if (ActorInfo->IsNetAuthority()) // 检查是否在网络权威端
	{
		UWorld* World = ActorInfo->AbilitySystemComponent->GetWorld(); // 获取世界对象
		ULyraGamePhaseSubsystem* PhaseSubsystem = UWorld::GetSubsystem<ULyraGamePhaseSubsystem>(World); // 获取游戏阶段子系统
		PhaseSubsystem->OnBeginPhase(this, Handle); // 通知子系统阶段开始
	}

	Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData); // 调用父类激活能力
}

/**
 * 结束能力时的具体实现
 */
void ULyraGamePhaseAbility::EndAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, bool bReplicateEndAbility, bool bWasCancelled)
{
	if (ActorInfo->IsNetAuthority()) // 检查是否在网络权威端
	{
		UWorld* World = ActorInfo->AbilitySystemComponent->GetWorld(); // 获取世界对象
		ULyraGamePhaseSubsystem* PhaseSubsystem = UWorld::GetSubsystem<ULyraGamePhaseSubsystem>(World); // 获取游戏阶段子系统
		PhaseSubsystem->OnEndPhase(this, Handle); // 通知子系统阶段结束
	}

	Super::EndAbility(Handle, ActorInfo, ActivationInfo, bReplicateEndAbility, bWasCancelled); // 调用父类结束能力
}

#if WITH_EDITOR
/**
 * 数据验证实现，检查游戏阶段标签是否有效
 */
EDataValidationResult ULyraGamePhaseAbility::IsDataValid(FDataValidationContext& Context) const
{
	EDataValidationResult Result = CombineDataValidationResults(Super::IsDataValid(Context), EDataValidationResult::Valid); // 组合父类验证结果

	if (!GamePhaseTag.IsValid()) // 检查游戏阶段标签是否有效
	{
		Result = EDataValidationResult::Invalid; // 设置为无效
		Context.AddError(LOCTEXT("GamePhaseTagNotSet", "GamePhaseTag must be set to a tag representing the current phase.")); // 添加错误信息
	}

	return Result; // 返回验证结果
}
#endif

#undef LOCTEXT_NAMESPACE // 取消定义本地化文本命名空间